﻿-- =============================================
-- Script 马双全 2012-7-11
-- =============================================
if exists(select * from sysobjects where name='proc_userlookexpertNews')
drop proc proc_userlookexpertNews
go
create procedure proc_userlookexpertNews
@userId int,--访问用户ID
@expertId int, --访问的专家信息ID
@UserColourMoney decimal(18,0),--用户的自身彩币
@caibi int,--需要访问的彩币数量
@sult  int out ---输出信息
as
 declare @userexpertId int;
 set  @userexpertId=(select count(mid) from UserLookExpertNewsMsg where userid=@userId and expertid=@expertid)
 if(@userexpertId>0)
 begin
 set @sult=1  --可以访问
 return
 end
 if(@UserColourMoney<@caibi)
 begin
 set @sult=-1  ---彩币不足
    return
 end
begin tran
	update  pm_users set UserColourMoney=(UserColourMoney-@caibi) where Id=@userId
	update pm_users set UserColourMoney=(UserColourMoney+@caibi) where Id=(select UserId from pm_expertNews where Id=@expertId)
	insert into UserLookExpertNewsMsg values(getdate(),@userId,@expertId,@caibi)
	if(@@error=0)
	  begin
	  set @sult=1--可以访问
	  commit tran
	  end
	  else
	  begin
	   set @sult=-2  ---系统繁忙
	   rollback tran
	  end 